/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.ui.customize;

import java.awt.Image;
import java.awt.Insets;
import java.awt.MediaTracker;

import javax.swing.ImageIcon;

import lineage.ui.L1Button;
import lineage.ui.util.L1LoadImage;
import lineage.util.Util;

/**
 * 自定义按钮-根据图片自适应大小.
 * 
 * @version 2014年2月21日下午3:23:54
 * @author jrwz
 */
public class L1CustomizeButton extends L1Button {
    private static final long serialVersionUID = -7477871885637914394L;
    /** 按钮默认的外形图片. */
    private Image img;
    /** 鼠标经过时的外形图片. */
    private Image imgOver;
    /** 按钮按下的外形图片. */
    private Image imgPressed;
    /** 按钮禁用时的外形图片. */
    private Image imgDisabled;
    /** 按钮默认的外形图片路径(图片必须是png格式). */
    protected String imgURL;
    /** 鼠标经过的外形图片路径(图片必须是png格式). */
    protected String imgOverURL;
    /** 按钮按下的外形图片路径(图片必须是png格式). */
    protected String imgPressedURL;
    /** 按钮禁用时的外形图片路径(图片必须是png格式). */
    protected String imgDisabledURL;
    /** 创建并初始化具有指定顶部、左边、底部、右边 inset 的新 Insets 对象. */
    protected Insets insets = new Insets(5, 18, 5, 5);

    protected L1CustomizeButton() {
    }

    @Override
    protected void init(boolean flag) {
        img = L1LoadImage.getInstance().getImg(imgURL);
        imgOver = L1LoadImage.getInstance().getImg(imgOverURL);
        imgPressed = L1LoadImage.getInstance().getImg(imgPressedURL);
        imgDisabled = L1LoadImage.getInstance().getImg(imgDisabledURL);
        if (img == null) {
            Util.message("没有找到图片(必须是png图片)：" + "(" + imgURL + ")");
            return;
        }
        if (imgOver == null) {
            Util.message("没有找到图片(必须是png图片)：" + "(" + imgOverURL + ")");
            return;
        }
        if (imgPressed == null) {
            Util.message("没有找到图片(必须是png图片)：" + "(" + imgPressedURL + ")");
            return;
        }
        if (imgDisabled == null) {
            Util.message("没有找到图片(必须是png图片)：" + "(" + imgDisabledURL + ")");
            return;
        }
        MediaTracker mt = new MediaTracker(this);
        mt.addImage(img, 0);
        mt.addImage(imgOver, 0);
        mt.addImage(imgPressed, 0);
        mt.addImage(imgDisabled, 0);
        try {
            mt.waitForAll(); // 开始加载由此媒体跟踪器跟踪的所有图像。此方法将等待，直到所有被跟踪的图像已经完成加载。
        } catch (InterruptedException e) {
            Util.message(e.getLocalizedMessage());
        }

        _width = img.getWidth(null);
        _height = img.getHeight(null);

        setMargin(insets); // 设置按钮边框和标签之间的间隔
        setContentAreaFilled(false); // 设置不绘制按钮的内容区域
        setBorderPainted(false); // 设置不绘制按钮的边框
        // setPreferredSize(new Dimension(_width, _height));
        setIcon(new ImageIcon(img)); // 设置默认情况下显示的按钮图片
        setRolloverIcon(new ImageIcon(imgOver)); // 设置当鼠标经过按钮时显示的图片
        setPressedIcon(new ImageIcon(imgPressed)); // 设置当按钮被按下时显示的图片
        setDisabledIcon(new ImageIcon(imgDisabled)); // 设置当按钮被禁用时显示的图片

        super.init(flag);
    }
}
